package com.springboot.hiresystem.dto.resp;

import lombok.Data;

import java.util.List;

@Data
public class Result<T> {

    private int code;
    private String message;
    private T data;
    private long timestamp;
    private int count; // 添加 count 字段

    public Result() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("请求成功");
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = success();
        result.setData(data);
        if (data instanceof List) {
            result.setCount(((List<?>) data).size()); // 设置 count 字段
        } else {
            result.setCount(1); // 如果不是列表，设置为 1
        }
        String message = data.toString();
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> failure(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}
